bzr branch
http://bzr.ed.am/stdhome
8
by Tim Marston
added diff command; moved all command to commands subdir; made stage-revert |
1 |
# init.py |
1
by Tim Marston
initial commit; basic app startup and initial command-line processing |
2 |
# |
3 |
# Copyright (C) 2013 Tim Marston <tim@edm.am> |
|
4 |
# |
|
5 |
# This file is part of stdhome (hereafter referred to as "this program"). |
|
6 |
# See http://ed.am/dev/stdhome for more information. |
|
7 |
# |
|
8 |
# This program is free software: you can redistribute it and/or modify |
|
9 |
# it under the terms of the GNU General Public License as published by |
|
10 |
# the Free Software Foundation, either version 3 of the License, or |
|
11 |
# (at your option) any later version. |
|
12 |
# |
|
13 |
# This program is distributed in the hope that it will be useful, |
|
14 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
16 |
# GNU General Public License for more details. |
|
17 |
# |
|
18 |
# You should have received a copy of the GNU General Public License |
|
19 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
20 |
||
21 |
||
2
by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented |
22 |
import sys, os, re, getopt, shutil, subprocess |
8
by Tim Marston
added diff command; moved all command to commands subdir; made stage-revert |
23 |
from command import Command |
24 |
import stdhome.the as the |
|
25 |
from stdhome.deployment import Deployment |
|
26 |
||
27 |
||
28 |
class InitCommand( Command ): |
|
1
by Tim Marston
initial commit; basic app startup and initial command-line processing |
29 |
|
2
by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented |
30 |
|
1
by Tim Marston
initial commit; basic app startup and initial command-line processing |
31 |
def print_help( self ): |
2
by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented |
32 |
print "Usage: " + the.program.name + " init [URL] [--repo=REPO]" |
1
by Tim Marston
initial commit; basic app startup and initial command-line processing |
33 |
|
34 |
# 01234567890123456789012345678901234567890123456789012345678901234567890123456789 |
|
35 |
print "Initialise a local repository." |
|
36 |
||
37 |
print "If an URL is given, the local reposity is a checkout of it (i.e., you can" |
|
38 |
print "receive updates from it and changes you commit will be sent to it). The URL" |
|
39 |
print "can take the form of a simple hostname, such as \"example.com\", or it can be a" |
|
40 |
print "fully-qualified bazaar URL. (Actually, in the first case, where it is a simple" |
|
41 |
print "hostname, it is internally expanded to scp://HOSTNAME/~/.stdhome/REPO)." |
|
42 |
||
43 |
print "Options:" |
|
61
by Tim Marston
added home directory change reporting to CopyOutWalker; added --quiet option to |
44 |
print " --quiet do not report changes to the home directory" |
2
by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented |
45 |
print " -r, --repo=REPO select the repo to check-out or create (defaults to 'home')" |
3
by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment |
46 |
print " -v, --verbose display information about what is being done" |
2
by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented |
47 |
print " --help display help and exit" |
1
by Tim Marston
initial commit; basic app startup and initial command-line processing |
48 |
exit( 0 ) |
49 |
||
50 |
||
2
by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented |
51 |
def parse_command_line( self ): |
52 |
opts, args = getopt.gnu_getopt( |
|
53 |
sys.argv[ 1: ], "r:v", |
|
61
by Tim Marston
added home directory change reporting to CopyOutWalker; added --quiet option to |
54 |
[ "quiet", "repo=", "verbose", "help" ] ) |
2
by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented |
55 |
for opt, optarg in opts: |
61
by Tim Marston
added home directory change reporting to CopyOutWalker; added --quiet option to |
56 |
if opt == "--quiet": |
57 |
self.quiet = True |
|
58 |
elif opt in [ '--repo', '-r' ]: |
|
59 |
if not re.search( '^[-a-zA-z0-9.]+$', optarg ): |
|
3
by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment |
60 |
raise the.program.FatalError( |
2
by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented |
61 |
'invalid repository name: ' + optarg ) |
17
by Tim Marston
read ~/.stdhomerc; commands set repo before run(); program performs late |
62 |
the.repo = optarg |
2
by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented |
63 |
elif opt == "--help": |
1
by Tim Marston
initial commit; basic app startup and initial command-line processing |
64 |
self.print_help() |
55
by Tim Marston
moved handling of --verbose to main program; manuall parse config file (because |
65 |
|
2
by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented |
66 |
# discard first argument (the command) |
67 |
args.pop( 0 ) |
|
68 |
||
69 |
# URL argument |
|
70 |
self.url = args[ 0 ].strip() if len( args ) else None |
|
71 |
||
35
by Tim Marston
made checks for verbose >= 1 explicit |
72 |
# check remaining arguments |
2
by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented |
73 |
if len( args ) > 1: |
74 |
raise the.program.UsageError( 'too many arguments' ) |
|
75 |
||
76 |
||
77 |
def run( self ): |
|
3
by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment |
78 |
|
79 |
# set up repo and check it *doesn't* already exists |
|
2
by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented |
80 |
the.repo.check_dir_exists( False ) |
81 |
||
82 |
# ensure our top-level directory exists |
|
3
by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment |
83 |
if not os.path.exists( the.full_dir ): |
84 |
os.mkdir( the.full_dir ) |
|
2
by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented |
85 |
|
3
by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment |
86 |
# checkout a remote repo, or create an empty local one? |
2
by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented |
87 |
if self.url: |
88 |
||
89 |
# expand url if it's a simple hostname |
|
61
by Tim Marston
added home directory change reporting to CopyOutWalker; added --quiet option to |
90 |
if re.search( '^[0-9a-zA-z.]+$', self.url ): |
45
by Tim Marston
switch to using bzr+ssh, rather than sftp (to work around broken pimiko) |
91 |
self.url = 'bzr+ssh://%s/%s/%s' % \ |
2
by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented |
92 |
( self.url, the.dir, the.repo.name ) |
93 |
||
94 |
# initialise deployment (with an empty repo) |
|
3
by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment |
95 |
deployment = Deployment() |
2
by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented |
96 |
deployment.copy_in() |
97 |
||
98 |
# perform bzr checkout |
|
35
by Tim Marston
made checks for verbose >= 1 explicit |
99 |
if the.verbose >= 1: print "checking out %s" % the.repo.dir |
3
by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment |
100 |
try: |
101 |
the.repo.vcs.checkout( self.url ) |
|
102 |
except Exception as e: |
|
2
by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented |
103 |
|
3
by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment |
104 |
# attempt to clean-up repo dir |
2
by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented |
105 |
try: |
3
by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment |
106 |
shutil.rmtree( the.repo.full_dir ) |
107 |
except OSError: |
|
2
by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented |
108 |
pass |
3
by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment |
109 |
|
110 |
raise e |
|
2
by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented |
111 |
|
60
by Tim Marston
fixed init command |
112 |
message = '' |
113 |
||
59
by Tim Marston
switched init command (and update command) to checking for conflicts prior to |
114 |
# check for deployment conclicts |
115 |
conflicts = deployment.get_conflicts() |
|
116 |
if conflicts: |
|
117 |
message += 'deployment conflicts:\n %s' % \ |
|
118 |
'\n '.join( conflicts ) |
|
119 |
||
60
by Tim Marston
fixed init command |
120 |
# stop if there are conflicts |
121 |
if message: |
|
122 |
raise the.program.FatalError( |
|
123 |
'there were conflicts...\n' + message ) |
|
124 |
||
2
by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented |
125 |
# perform deployment |
61
by Tim Marston
added home directory change reporting to CopyOutWalker; added --quiet option to |
126 |
deployment.copy_out( self.quiet ) |
2
by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented |
127 |
|
128 |
else: |
|
129 |
||
130 |
# perform bzr init |
|
35
by Tim Marston
made checks for verbose >= 1 explicit |
131 |
if the.verbose >= 1: print 'initialising %s' % the.repo.dir |
3
by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment |
132 |
try: |
133 |
the.repo.vcs.init() |
|
134 |
except Exception as e: |
|
2
by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented |
135 |
|
136 |
# attempt to clean-up repo dir, and die |
|
137 |
try: |
|
3
by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment |
138 |
shutil.rmtree( the.repo.full_dir ) |
139 |
except OSError: |
|
2
by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented |
140 |
pass |
3
by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment |
141 |
|
142 |
raise e |