Move delete function to util.py

This makes it easier to reuse in other places.
This commit is contained in:
Aneesh Agrawal 2017-04-16 16:01:54 -04:00
parent 7413c716fb
commit b1824f7a05
2 changed files with 14 additions and 12 deletions

View file

@ -33,13 +33,7 @@ from servo.command_base import (
is_windows, is_windows,
get_browserhtml_path, get_browserhtml_path,
) )
from servo.util import delete
def delete(path):
try:
os.remove(path) # Succeeds if path was a file
except OSError: # Or, if path was a directory...
shutil.rmtree(path) # Remove it and all its contents.
def otool(s): def otool(s):

View file

@ -10,16 +10,24 @@
from __future__ import absolute_import, print_function, unicode_literals from __future__ import absolute_import, print_function, unicode_literals
import os import os
import os.path as path import os.path
import platform import platform
import sys import shutil
from socket import error as socket_error from socket import error as socket_error
import StringIO import StringIO
import sys
import tarfile import tarfile
import zipfile import zipfile
import urllib2 import urllib2
def delete(path):
if os.path.isdir(path) and not os.path.islink(path):
shutil.rmtree(path)
else:
os.remove(path)
def host_platform(): def host_platform():
os_type = platform.system().lower() os_type = platform.system().lower()
if os_type == "linux": if os_type == "linux":
@ -126,7 +134,7 @@ def download_bytes(desc, src):
def download_file(desc, src, dst): def download_file(desc, src, dst):
tmp_path = dst + ".part" tmp_path = dst + ".part"
try: try:
start_byte = path.getsize(tmp_path) start_byte = os.path.getsize(tmp_path)
with open(tmp_path, 'ab') as fd: with open(tmp_path, 'ab') as fd:
download(desc, src, fd, start_byte=start_byte) download(desc, src, fd, start_byte=start_byte)
except os.error: except os.error:
@ -143,8 +151,8 @@ def extract(src, dst, movedir=None):
if movedir: if movedir:
for f in os.listdir(movedir): for f in os.listdir(movedir):
frm = path.join(movedir, f) frm = os.path.join(movedir, f)
to = path.join(dst, f) to = os.path.join(dst, f)
os.rename(frm, to) os.rename(frm, to)
os.rmdir(movedir) os.rmdir(movedir)