bzr branch
http://bzr.ed.am/stdhome
1
by Tim Marston
initial commit; basic app startup and initial command-line processing |
1 |
# command_init.py |
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 |
3
by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment |
23 |
import the |
24 |
from deployment import Deployment |
|
2
by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented |
25 |
from subprocess import Popen |
1
by Tim Marston
initial commit; basic app startup and initial command-line processing |
26 |
|
27 |
||
28 |
class CommandInit: |
|
29 |
||
2
by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented |
30 |
|
31 |
def __init__( self ): |
|
32 |
self.repo = None |
|
33 |
||
34 |
||
1
by Tim Marston
initial commit; basic app startup and initial command-line processing |
35 |
def print_help( self ): |
2
by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented |
36 |
print "Usage: " + the.program.name + " init [URL] [--repo=REPO]" |
1
by Tim Marston
initial commit; basic app startup and initial command-line processing |
37 |
|
38 |
# 01234567890123456789012345678901234567890123456789012345678901234567890123456789 |
|
39 |
print "Initialise a local repository." |
|
40 |
||
41 |
print "If an URL is given, the local reposity is a checkout of it (i.e., you can" |
|
42 |
print "receive updates from it and changes you commit will be sent to it). The URL" |
|
43 |
print "can take the form of a simple hostname, such as \"example.com\", or it can be a" |
|
44 |
print "fully-qualified bazaar URL. (Actually, in the first case, where it is a simple" |
|
45 |
print "hostname, it is internally expanded to scp://HOSTNAME/~/.stdhome/REPO)." |
|
46 |
||
47 |
print "Options:" |
|
2
by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented |
48 |
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 |
49 |
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 |
50 |
print " --help display help and exit" |
1
by Tim Marston
initial commit; basic app startup and initial command-line processing |
51 |
exit( 0 ) |
52 |
||
53 |
||
2
by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented |
54 |
def parse_command_line( self ): |
55 |
opts, args = getopt.gnu_getopt( |
|
56 |
sys.argv[ 1: ], "r:v", |
|
57 |
[ "repo=", "verbose", "help" ] ) |
|
58 |
for opt, optarg in opts: |
|
3
by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment |
59 |
if opt in [ '--repo', '-r' ]: |
2
by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented |
60 |
if not re.match( '^[-a-zA-z0-9.]+$', optarg ): |
3
by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment |
61 |
raise the.program.FatalError( |
2
by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented |
62 |
'invalid repository name: ' + optarg ) |
63 |
self.repo = optarg |
|
3
by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment |
64 |
elif opt in [ '--verbose', '-v' ]: |
2
by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented |
65 |
the.verbose = True |
66 |
elif opt == "--help": |
|
1
by Tim Marston
initial commit; basic app startup and initial command-line processing |
67 |
self.print_help() |
2
by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented |
68 |
|
69 |
# discard first argument (the command) |
|
70 |
args.pop( 0 ) |
|
71 |
||
72 |
# URL argument |
|
73 |
self.url = args[ 0 ].strip() if len( args ) else None |
|
74 |
||
75 |
# remaining arguments |
|
76 |
if len( args ) > 1: |
|
77 |
raise the.program.UsageError( 'too many arguments' ) |
|
78 |
||
79 |
||
80 |
def run( self ): |
|
3
by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment |
81 |
|
82 |
# 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 |
83 |
the.set_repo( self.repo ) |
84 |
the.repo.check_dir_exists( False ) |
|
85 |
||
86 |
# ensure our top-level directory exists |
|
3
by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment |
87 |
if not os.path.exists( the.full_dir ): |
88 |
os.mkdir( the.full_dir ) |
|
2
by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented |
89 |
|
3
by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment |
90 |
# 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 |
91 |
if self.url: |
92 |
||
93 |
# expand url if it's a simple hostname |
|
94 |
if re.match( '^[0-9a-zA-z.]+$', self.url ): |
|
95 |
self.url = 'sftp://%s/%s/%s' % \ |
|
96 |
( self.url, the.dir, the.repo.name ) |
|
97 |
||
98 |
# initialise deployment (with an empty repo) |
|
3
by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment |
99 |
deployment = Deployment() |
2
by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented |
100 |
deployment.copy_in() |
101 |
||
102 |
# perform bzr checkout |
|
3
by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment |
103 |
if the.verbose: print "checking out %s" % the.repo.dir |
104 |
try: |
|
105 |
the.repo.vcs.checkout( self.url ) |
|
106 |
except Exception as e: |
|
2
by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented |
107 |
|
3
by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment |
108 |
# attempt to clean-up repo dir |
2
by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented |
109 |
try: |
3
by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment |
110 |
shutil.rmtree( the.repo.full_dir ) |
111 |
except OSError: |
|
2
by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented |
112 |
pass |
3
by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment |
113 |
|
114 |
raise e |
|
2
by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented |
115 |
|
116 |
# perform deployment |
|
3
by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment |
117 |
try: |
118 |
deployment.copy_out() |
|
119 |
except deployment.Conflict as e: |
|
120 |
raise the.program.FatalError( e.msg ) |
|
2
by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented |
121 |
|
122 |
else: |
|
123 |
||
124 |
# perform bzr init |
|
3
by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment |
125 |
if the.verbose: print 'initialising %s' % the.repo.dir |
126 |
try: |
|
127 |
the.repo.vcs.init() |
|
128 |
except Exception as e: |
|
2
by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented |
129 |
|
130 |
# attempt to clean-up repo dir, and die |
|
131 |
try: |
|
3
by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment |
132 |
shutil.rmtree( the.repo.full_dir ) |
133 |
except OSError: |
|
2
by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented |
134 |
pass |
3
by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment |
135 |
|
136 |
raise e |