def test_push(self, m): m.side_effect = lambda *args, **kw: sarge.run(*args, **dict(kw, stdout=DEVNULL, stderr=DEVNULL)) repo_dir = os.path.join(self.tempd, 'normal_repo') bare_dir = os.path.join(self.tempd, 'bare_repo') os.makedirs(repo_dir) # Create git repo to be used as remote sarge.run('touch readme', cwd=repo_dir, stdout=DEVNULL) sarge.run('git init', cwd=repo_dir, stdout=DEVNULL) sarge.run('git add .', cwd=repo_dir, stdout=DEVNULL) sarge.run('git commit -m "Test"', cwd=repo_dir, stdout=DEVNULL) sarge.run('git clone --bare {} bare_repo'.format(repo_dir), cwd=self.tempd, stdout=DEVNULL) # First push with tempfile.TemporaryDirectory(prefix='test') as tmp: output_dir = os.path.join(tmp, 'copy', 'output') os.makedirs(output_dir) sarge.run('touch output.txt', cwd=output_dir, stdout=DEVNULL) push_doc(bare_dir, 'dev', 'Commit msg', 'output', ['exclude'], '', tmp) files = sarge.get_stdout('git ls-tree --name-only -r dev', cwd=bare_dir) self.assertTrue('output.txt' in files.split()) # Push again ... with tempfile.TemporaryDirectory(prefix='test') as tmp: output_dir = os.path.join(tmp, 'copy', 'output') os.makedirs(output_dir) sarge.run('touch output_2.txt', cwd=output_dir, stdout=DEVNULL) push_doc(bare_dir, 'dev', 'New msg', 'output', ['exclude'], '', tmp) out = sarge.get_stdout('git log dev -1 --pretty=format:%s', cwd=bare_dir) self.assertEqual(out, 'New msg') files = sarge.get_stdout('git ls-tree --name-only -r dev', cwd=bare_dir) self.assertTrue('output_2.txt' in files.split()) self.assertFalse('output.txt' in files.split())
def get_pylint_score(): """ Returns the pylint score. """ result = get_stdout('pylint {}'.format(MODULE)) score = findall(PYLINT_SCORE, result)[0] return float(score)
def get_current_session(server=None): ''' Seems to be no easy way to grab current attached session in tmuxp so this provides a simple alternative. ''' server = tmuxp.Server() if server is None else server session_name = sarge.get_stdout('tmux display-message -p "#S"').strip() session = server.findWhere({"session_name": session_name}) return session
def get_vcgencmd_throttled_state(command): output = sarge.get_stdout(command) #output = "throttled={}".format(next(_VCGENCMD_OUTPUT)) # for local debugging if not "throttled=0x" in output: raise ValueError("cannot parse \"{}\" output: {}".format(command, output)) value = output[len("throttled="):].strip(" \t\r\n\0") value = int(value, 0) return ThrottleState.from_value(value)
def check_docker_connection(self): output = get_stdout('docker images') # This checks if we can get a connection to the remote docker # server. It assumes the output of the "docker images"" command is # of the form: "Get http:///var/run/docker.sock/v1.19/images/json: dial # unix /var/run/docker.sock: no such file or directory. Are you trying # to connect to a TLS-enabled daemon without TLS?" if 'IMAGE ID' not in output: raise DockerServerError("Docker cannot connect to daemon")
def download(): tmp = sarge.get_stdout('mktemp').rstrip( ) # create temporary file and remove new line from file name logger.debug('temp file: {}'.format(tmp)) pl = 'PLOnNuoRw5c6XDortZHo3FOU-4luWsEZeD' logger.info('downloading playlist {0}: {1}'.format('swing_music', pl)) sarge.run( 'youtube-dl --get-title --no-warnings https://www.youtube.com/playlist?list={0} > {1}' .format(pl, tmp)) return tmp
def check_docker_version(self, min_version=min_docker_version): """check_docker_version makes sure docker is of a min version""" output = get_stdout('docker --version') # in docker 1.7.1 version is at 2 position in returned string version = output.split()[2] # remove comma from out put if in string if ',' in version: version = version[:-1] if self.ver_cmp(version, min_version) < 0: raise DockerInsuficientVersionError( "Please make sure docker is greater than %s" % min_version)
def stashed(): """ Stash and unstash non-indexed changes. This way code that won't be commited doesn't effect results. """ run('git stash save -q -u --keep-index "pre-commit"') yield stashes = get_stdout('git stash list | grep pre-commit') index = findall(STASH_INDEX, stashes) if index: run('git stash pop -q --index={}'.format(index[0]))
def reload(self, branch='master'): """ get or update plugins from git """ resp = [] old_cwd = os.getcwd() os.chdir(self.basedir) try: # clone for the first time if not os.path.exists(self._name): sarge.run('git clone %s' % self._repo) os.chdir(self._name) p = sarge.capture_both( 'git fetch origin && git checkout %s && git pull' % branch ) ok = True for code in p.returncodes: if code != 0: resp.extend([ 'sth went wrong when pulling plugins from %s:' % ( self._repo), p.stdout.text, p.stderr.text]) ok = False break if ok: resp.append('git plugins pulled from %s@%s: "%s"' % ( self._repo, sarge.get_stdout('git rev-parse HEAD').strip('\n')[:8], sarge.get_stdout('git log -1 --pretty=%B').strip('\n') )) if os.path.exists('requirements.txt'): pip = sarge.capture_both( 'pip install -r requirements.txt') if pip.returncode != 0: resp.extend([ 'sth went wrong when installing plugin requirements', pip.stdout.text, pip.stderr.text]) else: current_app.logger.info('file not found') return '\n'.join(resp) finally: os.chdir(old_cwd)
def _compute_sha256_digest(path): """Compute SHA256 digest of given path""" # Uses sha256sum, which returns e.g.: # 65ecde5d025fcf57ceaa32230e2ff884ab204065b86e0e34e609313c7bdc7b47 /etc/passwd input_path = str(path) data = sarge.get_stdout(["sha256sum", input_path]) digest, sep, result_path = data.partition(' ') if not sep or result_path.strip() != input_path: log.error(data) raise RuntimeError("can't compute digest of %s" % path) log.debug("%s -> sha256: %s", path, digest) return digest
def get_vcgencmd_throttled_state(command): if __LOCAL_DEBUG: output = "throttled={}".format(next(_VCGENCMD_OUTPUT)) # mock for local debugging else: output = sarge.get_stdout(command, close_fds=CLOSE_FDS) if "throttled=0x" not in output: raise ValueError('cannot parse "{}" output: {}'.format(command, output)) value = output[len("throttled=") :].strip(" \t\r\n\0") value = int(value, 0) return ThrottleState.from_value(value)
def reload(self, branch='master'): """ get or update plugins from git """ resp = [] old_cwd = os.getcwd() os.chdir(self.basedir) try: # clone for the first time if not os.path.exists(self._name): sarge.run('git clone %s' % self._repo) os.chdir(self._name) p = sarge.capture_both( 'git fetch origin && git checkout %s && git pull' % branch) ok = True for code in p.returncodes: if code != 0: resp.extend([ 'sth went wrong when pulling plugins from %s:' % (self._repo), p.stdout.text, p.stderr.text ]) ok = False break if ok: resp.append( 'git plugins pulled from %s@%s: "%s"' % (self._repo, sarge.get_stdout('git rev-parse HEAD').strip('\n')[:8], sarge.get_stdout('git log -1 --pretty=%B').strip('\n'))) if os.path.exists('requirements.txt'): pip = sarge.capture_both('%s install -r requirements.txt' % pip_bin) if pip.returncode != 0: resp.extend([ 'sth went wrong when installing plugin requirements', pip.stdout.text, pip.stderr.text ]) return '\n'.join(resp) finally: os.chdir(old_cwd)
def _read_srpm_metadata(srpm_path): """Read SRPM metadata using the `rpm` utility""" command = ['rpm', '--query', '--package', '--info', str(srpm_path)] log.debug("Reading SRPM metadata: %r", command) rpm_output = sarge.get_stdout(command) keys_of_interest = "Epoch Name Version Release".split() metadata = dict.fromkeys(map(str.lower, keys_of_interest)) for line in rpm_output.splitlines(): key, sep, value = line.partition(":") key = key.strip().lower() if key in metadata: metadata[key] = value.strip() return metadata
def test_main(self, m): m.side_effect = lambda *args, **kw: sarge.run(*args, **dict(kw, stdout=DEVNULL, stderr=DEVNULL)) # Create folders repo_dir = os.path.join(self.tempd, 'normal_repo') bare_dir = os.path.join(self.tempd, 'bare_repo') os.makedirs(repo_dir) os.makedirs(bare_dir) # Create git repo to simulate our real git repo sarge.run('touch readme', cwd=repo_dir, stdout=DEVNULL) sarge.run('git init', cwd=repo_dir, stdout=DEVNULL) sarge.run('git add .', cwd=repo_dir, stdout=DEVNULL) sarge.run('git commit -m "Test"', cwd=repo_dir, stdout=DEVNULL) # Create git repo to be used as remote sarge.run('git --bare init', cwd=bare_dir, stdout=DEVNULL, stderr=DEVNULL) sarge.run('git remote add origin {}'.format(bare_dir), cwd=repo_dir, stdout=DEVNULL, stderr=DEVNULL) sarge.run('git push origin master', cwd=repo_dir, stdout=DEVNULL, stderr=DEVNULL) # Create ini file config = ConfigParser() config['doc'] = {'command': 'mkdir output && touch output/index.html' ' && touch output/foo.html', 'output_folder': 'output', 'exclude': 'foo.html', 'extra': 'bar.html' } config['git'] = {'remote': '', 'service': 'bare_repo', 'branch': 'some_branch', 'message': 'Some message' } with open(os.path.join(repo_dir, 'd2g.ini'), 'w') as configfile: config.write(configfile) os.chdir(repo_dir) main() files = sarge.get_stdout('git ls-tree --name-only -r {}' .format(config['git']['branch']), cwd=bare_dir) self.assertTrue('index.html' in files.split()) self.assertTrue('bar.html' in files.split()) self.assertTrue('foo.html' not in files.split())
def _get_pb_power(self): import sarge command = ["gpio", "-g", "read", "2"] try: output = sarge.get_stdout(command) except: self._logger.exception("{} failed".format(" ".join(command))) else: if output.strip() == "1": return "off" elif output.strip() == "0": return "on" return "unknown"
def _get_source_paths(srpm_dir): """Gets the Specfile and all referenced SourceX files from `srpm_dir`""" specs = list(srpm_dir.glob("*.spec")) if len(specs) != 1: # sanity msg = "Expected exactly 1 specfile in {path}, found {num}" raise RuntimeError(msg.format(num=len(specs), path=srpm_dir)) # Source entry format: # Source0: http://pypi.python.org/packages/source/r/requests/requests-2.7.0.tar.gz # spec = specs.pop() source_urls = [] paths = [] log.debug("Querying specfile for sources: %s", spec) spectool_result = sarge.get_stdout(['spectool', '-S', str(spec)]) for src in spectool_result.splitlines(): name, url = src.split(" ", 1) source_urls.append(url) local_filename = basename(url) paths.append(srpm_dir / local_filename) log.debug("Local source paths: %s", paths) return spec, source_urls, paths
def hostname(): s = get_stdout("hostname") # print "hostname=%s" % (s.strip(), ) return s.strip()
def ipaddress(): s = get_stdout("hostname -I") a = s.split() # print "ipaddress=%s" % (a[0], ) return a[0]
def test_get_stdout(self): s = get_stdout('echo foo; echo bar') self.assertEqual(s.split(), ['foo', 'bar'])
def test_get_stdout(self): s = get_stdout("echo foo; echo bar") self.assertEqual(s.split(), ["foo", "bar"])
def get_scanners(): scanners = str(sarge.get_stdout("scanimage -f '%d%n'")).splitlines() return scanners
def get_stdout(command, **kwargs): if 'shell' not in kwargs: kwargs['shell'] = True sarge.get_stdout(command, **kwargs)