Skip to content

Commit

Permalink
Rebase hardening commits from dev
Browse files Browse the repository at this point in the history
  • Loading branch information
hexylena authored and natefoo committed Oct 12, 2017
1 parent 2ffeedf commit e4518ff
Show file tree
Hide file tree
Showing 44 changed files with 144 additions and 170 deletions.
6 changes: 3 additions & 3 deletions cron/build_chrom_db.py
Expand Up @@ -17,8 +17,8 @@
import os
import sys

import requests
from six.moves.urllib.parse import urlencode
from six.moves.urllib.request import urlopen

import parse_builds

Expand All @@ -36,8 +36,8 @@ def getchrominfo(url, db):
"hgta_regionType": "",
"position": "",
"hgta_doTopSubmit": "get info"})
page = urlopen(URL)
for line in page:
page = requests.get(URL).text
for line in page.split('\n'):
line = line.rstrip( "\r\n" )
if line.startswith("#"):
continue
Expand Down
5 changes: 2 additions & 3 deletions cron/parse_builds.py
Expand Up @@ -9,18 +9,17 @@
import sys
import xml.etree.ElementTree as ElementTree

from six.moves.urllib.request import urlopen
import requests


def getbuilds(url):
try:
page = urlopen(url)
text = requests.get(url).text
except:
print("#Unable to open " + url)
print("?\tunspecified (?)")
sys.exit(1)

text = page.read()
try:
tree = ElementTree.fromstring(text)
except:
Expand Down
6 changes: 3 additions & 3 deletions cron/parse_builds_3_sites.py
Expand Up @@ -6,7 +6,7 @@

import xml.etree.ElementTree as ElementTree

from six.moves.urllib.request import urlopen
import requests

sites = ['http://genome.ucsc.edu/cgi-bin/',
'http://archaea.ucsc.edu/cgi-bin/',
Expand All @@ -20,11 +20,11 @@ def main():
trackurl = sites[i] + "hgTracks?"
builds = []
try:
page = urlopen(site)
text = requests.get(site).text
except:
print("#Unable to connect to " + site)
continue
text = page.read()

try:
tree = ElementTree.fromstring(text)
except:
Expand Down
13 changes: 5 additions & 8 deletions lib/galaxy/datatypes/binary.py
Expand Up @@ -241,10 +241,8 @@ def merge(split_files, output_file):

def _is_coordinate_sorted( self, file_name ):
"""See if the input BAM file is sorted from the header information."""
params = [ "samtools", "view", "-H", file_name ]
output = subprocess.Popen( params, stderr=subprocess.PIPE, stdout=subprocess.PIPE ).communicate()[0]
# find returns -1 if string is not found
return output.find( "SO:coordinate" ) != -1 or output.find( "SO:sorted" ) != -1
output = subprocess.check_output(["samtools", "view", "-H", file_name])
return 'SO:coordinate' in output or 'SO:sorted' in output

def dataset_content_needs_grooming( self, file_name ):
"""See if file_name is a sorted BAM file"""
Expand All @@ -269,8 +267,7 @@ def dataset_content_needs_grooming( self, file_name ):
return False
index_name = tempfile.NamedTemporaryFile( prefix="bam_index" ).name
stderr_name = tempfile.NamedTemporaryFile( prefix="bam_index_stderr" ).name
command = 'samtools index %s %s' % ( file_name, index_name )
proc = subprocess.Popen( args=command, shell=True, stderr=open( stderr_name, 'wb' ) )
proc = subprocess.Popen(['samtools', 'index', file_name, index_name], stderr=open(stderr_name, 'wb'))
proc.wait()
stderr = open( stderr_name ).read().strip()
if stderr:
Expand Down Expand Up @@ -313,8 +310,8 @@ def groom_dataset_content( self, file_name ):
tmp_sorted_dataset_file_name_prefix = os.path.join( tmp_dir, 'sorted' )
stderr_name = tempfile.NamedTemporaryFile( dir=tmp_dir, prefix="bam_sort_stderr" ).name
samtools_created_sorted_file_name = "%s.bam" % tmp_sorted_dataset_file_name_prefix # samtools accepts a prefix, not a filename, it always adds .bam to the prefix
command = "samtools sort %s %s" % ( file_name, tmp_sorted_dataset_file_name_prefix )
proc = subprocess.Popen( args=command, shell=True, cwd=tmp_dir, stderr=open( stderr_name, 'wb' ) )
proc = subprocess.Popen(['samtools', 'sort', file_name, tmp_sorted_dataset_file_name_prefix],
cwd=tmp_dir, stderr=open(stderr_name, 'wb'))
exit_code = proc.wait()
# Did sort succeed?
stderr = open( stderr_name ).read().strip()
Expand Down
5 changes: 3 additions & 2 deletions lib/galaxy/datatypes/converters/interval_to_coverage.py
Expand Up @@ -132,8 +132,9 @@ def close(self):
# Sort through a tempfile first
temp_file = tempfile.NamedTemporaryFile(mode="r")
environ['LC_ALL'] = 'POSIX'
commandline = "sort -f -n -k %d -k %d -k %d -o %s %s" % (chr_col_1 + 1, start_col_1 + 1, end_col_1 + 1, temp_file.name, in_fname)
subprocess.check_call(commandline, shell=True)
subprocess.check_call([
'sort', '-f', '-n', '-k', chr_col_1 + 1, '-k', start_col_1 + 1, '-k', end_col_1 + 1, '-o', temp_file.name, in_fname
])

coverage = CoverageWriter( out_stream=open(out_fname, "a"),
chromCol=chr_col_2, positionCol=position_col_2,
Expand Down
6 changes: 3 additions & 3 deletions lib/galaxy/datatypes/converters/lped_to_pbed_converter.py
Expand Up @@ -72,9 +72,9 @@ def rgConv(inpedfilepath, outhtmlname, outfilepath, plink):
if not missval:
print('### lped_to_pbed_converter.py cannot identify missing value in %s' % pedf)
missval = '0'
cl = '%s --noweb --file %s --make-bed --out %s --missing-genotype %s' % (plink, inpedfilepath, outroot, missval)
p = subprocess.Popen(cl, shell=True, cwd=outfilepath)
p.wait() # run plink
subprocess.check_call([plink, '--noweb', '--file', inpedfilepath,
'--make-bed', '--out', outroot,
'--missing-genotype', missval], cwd=outfilepath)


def main():
Expand Down
3 changes: 1 addition & 2 deletions lib/galaxy/datatypes/converters/pbed_ldreduced_converter.py
Expand Up @@ -41,8 +41,7 @@ def pruneLD(plinktasks=[], cd='./', vclbase=[]):
for task in plinktasks: # each is a list
vcl = vclbase + task
with open(plog, 'w') as sto:
x = subprocess.Popen(' '.join(vcl), shell=True, stdout=sto, stderr=sto, cwd=cd)
x.wait()
subprocess.check_call(vcl, stdout=sto, stderr=sto, cwd=cd)
try:
lplog = open(plog, 'r').readlines()
lplog = [elem for elem in lplog if elem.find('Pruning SNP') == -1]
Expand Down
4 changes: 1 addition & 3 deletions lib/galaxy/datatypes/converters/pbed_to_lped_converter.py
Expand Up @@ -40,9 +40,7 @@ def rgConv(inpedfilepath, outhtmlname, outfilepath, plink):
"""
basename = os.path.split(inpedfilepath)[-1] # get basename
outroot = os.path.join(outfilepath, basename)
cl = '%s --noweb --bfile %s --recode --out %s ' % (plink, inpedfilepath, outroot)
p = subprocess.Popen(cl, shell=True, cwd=outfilepath)
p.wait() # run plink
subprocess.check_call([plink, '--noweb', '--bfile', inpedfilepath, '--recode', '--out', outroot], cwd=outfilepath)


def main():
Expand Down
4 changes: 2 additions & 2 deletions lib/galaxy/datatypes/sequence.py
Expand Up @@ -8,6 +8,7 @@
import os
import re
import string
import subprocess
from cgi import escape

from six import PY3
Expand Down Expand Up @@ -662,8 +663,7 @@ def process_split_file(data):
else:
commands = Sequence.get_split_commands_sequential(is_gzip(input_name), input_name, output_name, start_sequence, sequence_count)
for cmd in commands:
if 0 != os.system(cmd):
raise Exception("Executing '%s' failed" % cmd)
subprocess.check_call(cmd, shell=True)
return True
process_split_file = staticmethod(process_split_file)

Expand Down
13 changes: 5 additions & 8 deletions lib/galaxy/datatypes/tabular.py
Expand Up @@ -515,15 +515,12 @@ def merge( split_files, output_file):
Multiple SAM files may each have headers. Since the headers should all be the same, remove
the headers from files 1-n, keeping them in the first file only
"""
cmd = 'mv %s %s' % ( split_files[0], output_file )
result = os.system(cmd)
if result != 0:
raise Exception('Result %s from %s' % (result, cmd))
shutil.move(split_files[0], output_file)

if len(split_files) > 1:
cmd = 'egrep -v -h "^@" %s >> %s' % ( ' '.join(split_files[1:]), output_file )
result = os.system(cmd)
if result != 0:
raise Exception('Result %s from %s' % (result, cmd))
cmd = ['egrep', '-v', '-h', '^@'] + split_files[1:] + ['>>', output_file]
subprocess.check_call(cmd, shell=True)

merge = staticmethod(merge)

# Dataproviders
Expand Down
11 changes: 6 additions & 5 deletions lib/galaxy/datatypes/text.py
Expand Up @@ -10,6 +10,8 @@
import subprocess
import tempfile

from six.moves import shlex_quote

from galaxy.datatypes.data import get_file_peek, Text
from galaxy.datatypes.metadata import MetadataElement, MetadataParameter
from galaxy.datatypes.sniff import get_headers
Expand Down Expand Up @@ -144,13 +146,12 @@ def _display_data_trusted(self, trans, dataset, preview=False, filename=None, to
ofilename = ofile_handle.name
ofile_handle.close()
try:
cmd = 'ipython nbconvert --to html --template full %s --output %s' % (dataset.file_name, ofilename)
log.info("Calling command %s" % cmd)
subprocess.call(cmd, shell=True)
cmd = ['ipython', 'nbconvert', '--to', 'html', '--template', 'full', dataset.file_name, '--output', ofilename]
subprocess.check_call(cmd)
ofilename = '%s.html' % ofilename
except:
except subprocess.CalledProcessError:
ofilename = dataset.file_name
log.exception( 'Command "%s" failed. Could not convert the IPython Notebook to HTML, defaulting to plain text.' % cmd )
log.exception('Command "%s" failed. Could not convert the IPython Notebook to HTML, defaulting to plain text.', ' '.join(map(shlex_quote, cmd)))
return open( ofilename )

def set_meta( self, dataset, **kwd ):
Expand Down
4 changes: 2 additions & 2 deletions lib/galaxy/external_services/actions.py
@@ -1,6 +1,6 @@
# Contains actions that are used in External Services
import logging
from urllib import urlopen
import requests
from galaxy.web import url_for
from galaxy.util.template import fill_template
from result_handlers.basic import ExternalServiceActionResultHandler
Expand Down Expand Up @@ -104,7 +104,7 @@ def __init__( self, name, param_dict, url, method, target ): # display_handler
@property
def content( self ):
if self._content is None:
self._content = urlopen( self.url ).read()
self._content = requests.get(self.url).text
return self._content


Expand Down
8 changes: 5 additions & 3 deletions lib/galaxy/jobs/deferred/pacific_biosciences_smrt_portal.py
Expand Up @@ -2,13 +2,15 @@
Module for managing jobs in Pacific Bioscience's SMRT Portal and automatically transferring files
produced by SMRT Portal.
"""
import json
import logging
import urllib2
from string import Template

import requests

from data_transfer import DataTransfer


log = logging.getLogger( __name__ )

__all__ = [ 'SMRTPortalPlugin' ]
Expand Down Expand Up @@ -87,8 +89,8 @@ def check_job( self, job ):
if self._missing_params( job.params, [ 'smrt_host', 'smrt_job_id' ] ):
return self.job_states.INVALID
url = 'http://' + job.params[ 'smrt_host' ] + self.api_path + '/Jobs/' + job.params[ 'smrt_job_id' ] + '/Status'
r = urllib2.urlopen( url )
status = json.loads( r.read() )
r = requests.get(url)
status = r.json()
# TODO: error handling: unexpected json or bad response, bad url, etc.
if status[ 'Code' ] == 'Completed':
log.debug( "SMRT Portal job '%s' is Completed. Initiating transfer." % job.params[ 'smrt_job_id' ] )
Expand Down
6 changes: 3 additions & 3 deletions lib/galaxy/jobs/runners/pulsar.py
Expand Up @@ -8,6 +8,7 @@
import errno
import logging
import os
import subprocess
from time import sleep

from pulsar.client import build_client_manager
Expand Down Expand Up @@ -210,7 +211,7 @@ def __init_pulsar_app( self, pulsar_conf_path ):
else:
log.info("Loading Pulsar app configuration from %s" % pulsar_conf_path)
with open(pulsar_conf_path, "r") as f:
conf.update(yaml.load(f) or {})
conf.update(yaml.safe_load(f) or {})
if "job_metrics_config_file" not in conf:
conf["job_metrics"] = self.app.job_metrics
if "staging_directory" not in conf:
Expand Down Expand Up @@ -375,8 +376,7 @@ def __prepare_input_files_locally(self, job_wrapper):
prepare_input_files_cmds = getattr(job_wrapper, 'prepare_input_files_cmds', None)
if prepare_input_files_cmds is not None:
for cmd in prepare_input_files_cmds: # run the commands to stage the input files
if 0 != os.system(cmd):
raise Exception('Error running file staging command: %s' % cmd)
subprocess.check_call(cmd, shell=True)
job_wrapper.prepare_input_files_cmds = None # prevent them from being used in-line

def _populate_parameter_defaults( self, job_destination ):
Expand Down
5 changes: 2 additions & 3 deletions lib/galaxy/jobs/runners/util/job_script/__init__.py
Expand Up @@ -117,9 +117,8 @@ def _handle_script_integrity(path, config):
sleep_amt = getattr(config, "check_job_script_integrity_sleep", DEFAULT_INTEGRITY_SLEEP)
for i in range(count):
try:
proc = subprocess.Popen([path], shell=True, env={"ABC_TEST_JOB_SCRIPT_INTEGRITY_XYZ": "1"})
proc.wait()
if proc.returncode == 42:
returncode = subprocess.call([path], env={"ABC_TEST_JOB_SCRIPT_INTEGRITY_XYZ": "1"})
if returncode == 42:
script_integrity_verified = True
break

Expand Down
5 changes: 3 additions & 2 deletions lib/galaxy/jobs/runners/util/kill.py
@@ -1,4 +1,5 @@
import os
import subprocess
from platform import system
from time import sleep
from subprocess import Popen
Expand Down Expand Up @@ -41,8 +42,8 @@ def _stock_kill_pid(pid):

def __kill_windows(pid):
try:
Popen("taskkill /F /T /PID %i" % pid, shell=True)
except Exception:
subprocess.check_call(['taskkill', '/F', '/T', '/PID', pid])
except subprocess.CalledProcessError:
pass


Expand Down
12 changes: 7 additions & 5 deletions lib/galaxy/jobs/transfer_manager.py
Expand Up @@ -9,6 +9,8 @@
import subprocess
import threading

from six.moves import shlex_quote

from galaxy.util import listify, sleeper
from galaxy.util.json import jsonrpc_request, validate_jsonrpc_response

Expand All @@ -22,8 +24,8 @@ class TransferManager( object ):
def __init__( self, app ):
self.app = app
self.sa_session = app.model.context.current
self.command = 'python %s' % os.path.abspath( os.path.join( os.getcwd(), 'scripts', 'transfer.py' ) )
if app.config.get_bool( 'enable_job_recovery', True ):
self.command = ['python', os.path.abspath(os.path.join(os.getcwd(), 'scripts', 'transfer.py'))]
if app.config.get_bool('enable_job_recovery', True):
# Only one Galaxy server process should be able to recover jobs! (otherwise you'll have nasty race conditions)
self.running = True
self.sleeper = sleeper.Sleeper()
Expand Down Expand Up @@ -67,9 +69,9 @@ def run( self, transfer_jobs ):
# The transfer script should daemonize fairly quickly - if this is
# not the case, this process will need to be moved to a
# non-blocking method.
cmd = '%s %s' % ( self.command, tj.id )
log.debug( 'Transfer command is: %s' % cmd )
p = subprocess.Popen( cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT )
cmd = self.command + [tj.id]
log.debug('Transfer command is: %s', ' '.join(map(shlex_quote, cmd)))
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
p.wait()
output = p.stdout.read( 32768 )
if p.returncode != 0:
Expand Down
8 changes: 3 additions & 5 deletions lib/galaxy/managers/citations.py
@@ -1,6 +1,6 @@
import functools
import os
import urllib2
import requests

from beaker.cache import CacheManager
from beaker.util import parse_cache_config_options
Expand Down Expand Up @@ -47,10 +47,8 @@ def __init__( self, config ):
def _raw_get_bibtex( self, doi ):
dx_url = "http://dx.doi.org/" + doi
headers = {'Accept': 'text/bibliography; style=bibtex, application/x-bibtex'}
req = urllib2.Request(dx_url, data="", headers=headers)
response = urllib2.urlopen(req)
bibtex = response.read()
return bibtex
req = requests.get(dx_url, headers=headers)
return req.text

def get_bibtex( self, doi ):
createfunc = functools.partial(self._raw_get_bibtex, doi)
Expand Down
9 changes: 4 additions & 5 deletions lib/galaxy/objectstore/s3.py
Expand Up @@ -13,7 +13,7 @@
from datetime import datetime

from galaxy.exceptions import ObjectNotFound, ObjectInvalid
from galaxy.util import string_as_bool, umask_fix_perms, safe_relpath, directory_hash_id
from galaxy.util import string_as_bool, umask_fix_perms, safe_relpath, directory_hash_id, which
from galaxy.util.sleeper import Sleeper
from .s3_multipart_upload import multipart_upload
from ..objectstore import ObjectStore, convert_bytes
Expand Down Expand Up @@ -59,10 +59,9 @@ def __init__(self, config, config_xml):
self.cache_monitor_thread.start()
log.info("Cache cleaner manager started")
# Test if 'axel' is available for parallel download and pull the key into cache
try:
subprocess.call('axel')
if which('axel'):
self.use_axel = True
except OSError:
else:
self.use_axel = False

def _configure_connection(self):
Expand Down Expand Up @@ -333,7 +332,7 @@ def _download(self, rel_path):
log.debug("Parallel pulled key '%s' into cache to %s", rel_path, self._get_cache_path(rel_path))
ncores = multiprocessing.cpu_count()
url = key.generate_url(7200)
ret_code = subprocess.call("axel -a -n %s '%s'" % (ncores, url))
ret_code = subprocess.call(['axel', '-a', '-n', ncores, url])
if ret_code == 0:
return True
else:
Expand Down

0 comments on commit e4518ff

Please sign in to comment.