eobuilder/eobuilder/cmdline.py

109 lines
3.3 KiB
Python

import os
import shutil
import subprocess
import sys
from optparse import OptionParser
from optparse import Option
class MultipleOption(Option):
ACTIONS = Option.ACTIONS + ("extend",)
STORE_ACTIONS = Option.STORE_ACTIONS + ("extend",)
TYPED_ACTIONS = Option.TYPED_ACTIONS + ("extend",)
ALWAYS_TYPED_ACTIONS = Option.ALWAYS_TYPED_ACTIONS + ("extend",)
def take_action(self, action, dest, opt, value, values, parser):
if action == "extend":
lvalue = value.split(",")
values.ensure_value(dest, []).extend(lvalue)
else:
Option.take_action(
self, action, dest, opt, value, values, parser)
def parse_cmdline():
parser = OptionParser(option_class=MultipleOption,
usage='usage: %prog [OPTIONS] -d [wheezy,squeeze|wheezy|squeeze] GIT_REPOSITORY_NAME')
parser.add_option("-a", "--architectures",
action="extend", type="string",
dest="architectures", metavar='ARCHITECTURES',
default=[],
help="ARCHITECTURES: amd64 and / or i368 (Default: amd64)")
parser.add_option("-d", "--distribution",
action="extend", type="string",
dest="distrib", metavar='DISTRIBUTIONS',
help="DISTRIBUTIONS: squeeze and / or wheezy")
parser.add_option("-b", "--branch",
action="store", type="string",
dest="branch", metavar='NAME',
default="master",
help="branch to build (Default: master)")
parser.add_option("-c", "--clean",
action="extend", type="string",
dest="cleanning", metavar='CLEANNING_METHODS',
default=[],
help="CLEANNING_METHODS: git, deb, archives, smart and / or all")
(options, args) = parser.parse_args()
if len(args) != 1 and not options.cleanning:
parser.error("you should select one GIT_REPOSITORY_NAME")
if len(args) and options.cleanning:
parser.error("you shouldn't use argument when cleanning")
if len(args) and not options.distrib:
parser.error("you should set option --distribution")
if not options.architectures:
options.architectures = ["amd64"]
return options, args
def cat(file_path):
with open(file_path, 'r') as f:
content = f.read()
f.close()
return content
def touch(fname):
with file(fname, 'a'):
os.utime(fname, None)
def error(msg, build_dir=None):
if build_dir:
shutil.rmtree(build_dir)
sys.stderr.write("ERROR: %s\n" % msg)
sys.exit(1)
class Execute(object):
def __init__(self, build_dir=None):
self.build_dir = build_dir
def call(self, cmd):
""" cmd: command line
"""
rcode = subprocess.call(cmd, shell=True)
if rcode != 0:
error(cmd, self.build_dir)
def output(self, cmd, print_output=False):
""" cmd: command line
print_output: print stdout and stderr
return outputs (stderr + stdout) as byte string
"""
output = None
try:
output = subprocess.check_output(cmd,
stderr=subprocess.STDOUT,
shell=True)
except subprocess.CalledProcessError, e:
sys.stderr.write(e.output)
error(cmd, self.build_dir)
if print_output:
print output
return output