def common(self): r1 = self.check() assert ex('systemctl stop systemd-sysctl.service').re() == 0 assert ex('systemctl start systemd-sysctl.service').re() == 0 if self.check() and r1: success() else: fail()
def test_io_redirect_using_gt_operator_file_arg(self): shell.ex('rm -rf {0}'.format(self.test_out_file)) with open(self.test_out_file, 'wb') as fd: shell.ex('echo 123') > fd out_content = shell.ex('cat {0}'.format(self.test_out_file)).stdout() self.assertEqual(b'123\n', out_content) os.remove(self.test_out_file)
def test_io_redirect_rshift_textio_arg(self): buffer = io.StringIO() shell.ex('echo 1') > buffer shell.ex('echo 123').ap(buffer) buffer.seek(0) out_content = buffer.read() self.assertEqual('1\n123\n', out_content)
def test_single_run_stderr(self): bad_path = 'wtf#noneexist#dir#yay' expected_stderr = subprocess.Popen( ['ls', bad_path], stderr=subprocess.PIPE).communicate()[1] task = shell.ex('ls {0}'.format(bad_path)) self.assertNotEqual(task.re(), 0) self.assertEqual(task.stderr(), expected_stderr)
def test_io_redirect_wr_string_arg(self): shell.ex('rm -rf {0}'.format(self.test_out_file)) shell.ex('echo 123456').wr(self.test_out_file) out_content = shell.ex('cat {0}'.format(self.test_out_file)).stdout() self.assertEqual(b'123456\n', out_content) shell.ex('echo 1').wr(self.test_out_file) out_content = shell.ex('cat {0}'.format(self.test_out_file)).stdout() self.assertEqual(b'1\n', out_content) os.remove(self.test_out_file)
def test_io_redirect_rshift_string_arg(self): shell.ex('rm -rf {0}'.format(self.test_out_file)) shell.ex('echo 1') >> self.test_out_file self.assertEqual( shell.ex('cat {0}'.format(self.test_out_file)).stdout(), b'1\n') shell.ex('echo 23') >> self.test_out_file self.assertEqual( shell.ex('cat {0}'.format(self.test_out_file)).stdout(), b'1\n23\n') os.remove(self.test_out_file)
def check_requirements(l): l.debug("checking if lftp is installed") which_lftp = ex("which lftp") if which_lftp.re() != 0: l.debug(which_lftp.stdout()) raise RuntimeError("lftp was not found") lftp_path = str(which_lftp.stdout(), "utf-8").strip() return LFTP(exe=lftp_path)
def find_changed_dockerfiles(): """ This function returns a list of changed dockerfiles since the last push on the current branch. It uses circleci API to fetch the list of new commits (compare string) and uses git diff/show to figue out what files has been changed. """ # STEP 1: find out the git compare string build_num = os.environ.get('CIRCLE_BUILD_NUM') info_api = 'https://circleci.com/api/v1.1/project/github/%s/%s/%s?circle-token=%s' % ( os.environ.get('CIRCLE_PROJECT_USERNAME'), os.environ.get('CIRCLE_PROJECT_REPONAME'), build_num, os.environ.get('CIRCLE_TOKEN') ) print('[*] Fetching build info from %s...' % info_api) re = requests.get(info_api) if re.status_code != 200: sys.exit('Failed to fetch metadata for current build!') build_info = re.json() # value of 'compare' key is a github compare URL, we are only interested in # the last part, i.e. the actual compare string git_compare_url = build_info['compare'] if not git_compare_url: return [] git_compare = git_compare_url.split('/')[-1] # STEP 2: construct command to generate changed list changed_dockerfiles = [] print('[*] List of changed files since last push (%s):' % git_compare) # multiple commits will be in the form of '2d12a44065cc^...aa673a945a6a' if '...' in git_compare: # use git diff for multiple commits cmd = 'git diff --name-only %s' % git_compare else: # use git show for single commit cmd = 'git show --name-only %s' % git_compare print('Running comand: %s' % cmd) # STEP 3: iterate through list of changed files and filter # based on filename for l in ex(cmd).stdout().split(): print(l) # only record dockerfiles if 'Dockerfile' in l: # TODO: support base image if 'dl/dl-base' in l: print('FIXME: Skipping base image: %s' % l) continue if os.path.exists(l.strip()): changed_dockerfiles.append(l) return changed_dockerfiles
def test_io_redirect_wr_file_arg(self): shell.ex('rm -rf {0}'.format(self.test_out_file)) with open(self.test_out_file, 'ab') as fd: shell.ex('echo 123').wr(fd) out_content = shell.ex('cat {0}'.format(self.test_out_file)).stdout() self.assertEqual(b'123\n', out_content) with open(self.test_out_file, 'ab') as fd: shell.ex('echo 1').wr(fd) out_content = shell.ex('cat {0}'.format(self.test_out_file)).stdout() self.assertEqual(b'1\n', out_content) os.remove(self.test_out_file)
def test_io_redirect_rshift_file_arg(self): shell.ex('rm -rf {0}'.format(self.test_out_file)) shell.ex('echo 1') > self.test_out_file with open(self.test_out_file, 'rb+') as fd: shell.ex('echo 23') >> fd self.assertEqual( shell.ex('cat {0}'.format(self.test_out_file)).stdout(), b'1\n23\n') os.remove(self.test_out_file)
def minifyFilesInDirectory(directory, ext, url): sameSize = '{:<16}' os.mkdir(FOLDER + '/' + directory) for root, dirs, files in os.walk(directory): for name in files: print('\n⧖ Minifying ' + '\x1b[1;32;33m' + sameSize.format(name) + '\x1b[0m ...', end='') chars = len(ext) if name.endswith('.min' + ext): shutil.copyfile(os.path.join(root, name), FOLDER + '/' + directory + '/' + name) print('\r✓ Copied ' + '\x1b[1;32;33m' + sameSize.format(name) + '\x1b[0m -> \x1b[1;32;32m' + name[:-chars] + '.min' + ext + '\x1b[0m', end='', flush=True) elif ext == '.js' and not name.endswith('.min' + ext): ex('uglifyjs ' + directory + '/' + name + ' -c -m -o ' + FOLDER + '/' + directory + '/' + name[:-chars] + '.min' + ext).stdout() replaceInHTMLFiles(name, name[:-chars] + '.min' + ext) replaceInManfest(name, name[:-chars] + '.min' + ext) print('\r✓ Minified ' + '\x1b[1;32;33m' + sameSize.format(name) + '\x1b[0m -> \x1b[1;32;32m' + name[:-chars] + '.min' + ext + '\x1b[0m', end='', flush=True) elif ext == '.css' and not name.endswith('.min' + ext): ex('csso ' + directory + '/' + name + ' -o ' + FOLDER + '/' + directory + '/' + name[:-chars] + '.min' + ext).stdout() replaceInHTMLFiles(name, name[:-chars] + '.min' + ext) print('\r✓ Minified ' + '\x1b[1;32;33m' + sameSize.format(name) + '\x1b[0m -> \x1b[1;32;32m' + name[:-chars] + '.min' + ext + '\x1b[0m', end='', flush=True)
def test_expand_tilde(self): task = shell.ex('echo ~') self.assertEqual(task.re(), 0) self.assertEqual(task.stdout(), os.path.expanduser('~') + '\n')
def test_ex_pipe(self): out = shell.ex('echo -e "123\n456\n789"').p('grep 4').stdout() self.assertEqual(out, '456\n')
def test_expand_environment_var(self): self.assertNotEqual(shell.env('foo'), 'barbar') os.environ['foo'] = 'barbar' self.assertEqual(shell.ex('echo $foo').stdout(), b'barbar\n')
def test_io_redirect_using_gt_operator_textio_arg(self): buffer = io.StringIO() shell.ex('echo 123').wr(buffer) buffer.seek(0) out_content = buffer.read() self.assertEqual('123\n', out_content)
from shell import ex if 'linux' in sys.platform: # start xvfb in case no X is running. Make sure xvfb # is installed, otherwise this won't work! dryscrape.start_xvfb() search_term = 'dryscrape' # set up a web scraping session sess = dryscrape.Session(base_url=sys.argv[1]) # we don't need images sess.set_attribute('auto_load_images', False) url = sys.argv[1] sess.visit(url) # extract all links for link in sess.xpath('//iframe[@ng-src]'): print(link['ng-src']) url = (link['ng-src']) print url bashCommand = 'youtube-dl -v'.encode('utf-8'), url.encode( 'utf-8'), '--referer http://itpro.tv'.encode('utf-8') bashCommand = " ".join(bashCommand) print bashCommand ex(bashCommand).stdout() #output = subprocess.check_output(['bash','-c', bashCommand])
def test_expand_tilde(self): task = shell.ex('echo ~') self.assertEqual(task.re(), 0) self.assertEqual(task.stdout(), os.path.expanduser('~')+'\n')
def test_io_redirect_using_gt_operator_string_arg(self): shell.ex('rm -rf {0}'.format(self.test_out_file)) shell.ex('echo 123') > self.test_out_file out_content = shell.ex('cat {0}'.format(self.test_out_file)).stdout() self.assertEqual(b'123\n', out_content) os.remove(self.test_out_file)
import time from shell import ex from shell import pipe_all kubecfg = ("~/code/source/go-workspace/src/github.com/GoogleCloudPlatform/" "kubernetes/cluster/kubecfg.sh ") controllercfg = ( "-c ~/code/source/go-workspace/src/github.com/GoogleCloudPlatform/" "kubernetes/examples/guestbook/frontend-controller.json ") for i in range(50): print kubecfg + controllercfg + "create replicationControllers" ex(kubecfg + controllercfg + "create replicationControllers").stdout() print "________________Created________________________________" print ex(kubecfg + "list pods").stdout() time.sleep(10) print ex(kubecfg + "list pods").stdout() time.sleep(10) print ex(kubecfg + "list pods").stdout() time.sleep(10) print ex(kubecfg + "list pods").stdout() time.sleep(10) re = pipe_all([kubecfg + "list pods", "sed -n 6,8p", "awk '{print $1}'"]).stdout() ex(kubecfg + "delete replicationControllers/frontendController").stdout() for line in re.split('\n'): if line: print kubecfg + "delete pods/" + line
def test_no_stdout_pipe(self): shell.ex('true').p('grep 1').stdout()
def Ubuntu(self): r1 = self.check() assert ex('start procps').re() == 0 if self.check() and r1: success() else: fail()
def test_single_run_stdout(self): re = shell.p('echo hello shell.py').stdout() self.assertEqual(re, 'hello shell.py\n') re = shell.ex('echo 你好 shell.py').stdout() self.assertEqual(re, '你好 shell.py\n')
def test_quote_string(self): self.assertEqual(shell.ex('echo "8888"').stdout(), b"8888\n")
import time from shell import ex from shell import pipe_all kubecfg = ("~/code/source/go-workspace/src/github.com/GoogleCloudPlatform/" "kubernetes/cluster/kubecfg.sh ") controllercfg = ("-c ~/code/source/go-workspace/src/github.com/GoogleCloudPlatform/" "kubernetes/examples/guestbook/frontend-controller.json ") for i in range(50): print kubecfg + controllercfg + "create replicationControllers" ex(kubecfg + controllercfg + "create replicationControllers").stdout() print "________________Created________________________________" print ex(kubecfg + "list pods").stdout() time.sleep(10) print ex(kubecfg + "list pods").stdout() time.sleep(10) print ex(kubecfg + "list pods").stdout() time.sleep(10) print ex(kubecfg + "list pods").stdout() time.sleep(10) re = pipe_all([kubecfg + "list pods", "sed -n 6,8p", "awk '{print $1}'"]).stdout() ex(kubecfg + "delete replicationControllers/frontendController").stdout() for line in re.split('\n'): if line: print kubecfg + "delete pods/" + line ex(kubecfg + "delete pods/" + line).stdout() time.sleep(1)
def get_calleridname(peernum): calleridname = (ex("asterisk -rx 'sip show peer %s'" % peernum) | "grep Callerid" | "awk '{print $3}'").stdout() return calleridname.replace('"', '')
def common(self): assert ex('usermod --shell /bin/bash projectmanager')
def common(self): ex('useradd projectmanager') assert ex('usermod --shell /bin/false projectmanager').re() == 0