from command.default import ManagerDefaultController
from cement.core.controller import expose
from util.config import Configuration
from urllib.request import urlretrieve
from subprocess import call
import os
import stat
import tarfile


class DownloadController(ManagerDefaultController):
    steamcmd_url = 'https://steamcdn-a.akamaihd.net/client/installer/steamcmd_linux.tar.gz'
    steamcmd_archive = '/steamcmd.tar.gz'
    ql_appid = 349090

    class Meta:
        label = 'download'
        description = 'Allows to download/update SteamCMD and QL Dedicated Server files'

    @expose(hide=True)
    def default(self):
        self.app.args.parse_args(['--help'])

    @expose(help='Downloads and installs SteamCMD')
    def steamcmd(self):
        config = Configuration()

        steamcmd_dir = os.path.expanduser(config.get('dir', 'steamcmd'))

        #check if steamcmd dir exists
        if os.path.isdir(steamcmd_dir):
            print(steamcmd_dir + ' exists. Remove it or change SteamCMD location with "setup" command')
            exit(30)

        os.makedirs(steamcmd_dir)

        if not os.access(steamcmd_dir, os.W_OK):
            print('Cannot get write access to ' + steamcmd_dir)
            exit(31)

        print('Downloading SteamCMD archive')
        urlretrieve(self.steamcmd_url, steamcmd_dir + self.steamcmd_archive)

        print('Extracting SteamCMD atchive to ' + steamcmd_dir)
        archive = tarfile.open(steamcmd_dir + self.steamcmd_archive)
        archive.extractall(steamcmd_dir)

        steamcmd_stat = os.stat(steamcmd_dir + '/steamcmd.sh')
        os.chmod(steamcmd_dir + '/steamcmd.sh', steamcmd_stat.st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)

        print('SteamCMD installed in ' + steamcmd_dir)
        print('Remember that you need "lib32stdc++6" installed in your system')

    @expose(help='Downloads and updates QL Dedicated Server files')
    def ql(self):
        config = Configuration()

        steamcmd = os.path.expanduser(config.get('dir', 'steamcmd') + '/steamcmd.sh')

        if not os.path.exists(steamcmd):
            print('SteamCMD executable doesn\'t exist. Install SteamCMD first')
            exit(32)

        if not os.access(steamcmd, os.X_OK):
            print('SteamCMD script (' + steamcmd + ') is not executable')
            exit(31)

        print('Downloading QL Dedicated Server files using SteamCMD...')

        call([
            steamcmd,
            '+login anonymous +force_install_dir %s +app_update %d +quit' % (config.get('dir', 'ql'), self.ql_appid)
        ])
@@ -126,16 +126,52 @@
 uration%0A
+from util.filesystem import FSCheck%0A
 from url
@@ -884,16 +884,80 @@
 amcmd'))
+%0A        steamcmd_dir_fs = FSCheck(steamcmd_dir, 'SteamCMD dir')
 %0A%0A      
@@ -1003,34 +1003,42 @@
  if 
-os.path.isdir(steamcmd_dir
+steamcmd_dir_fs.exists(error=False
 ):%0A 
@@ -1054,32 +1054,18 @@
   print(
-steamcmd_dir + 
 '
+%25
  exists.
@@ -1107,29 +1107,35 @@
 ion 
-with %22setup%22 command'
+in settings' %25 steamcmd_dir
 )%0A  
@@ -1153,10 +1153,10 @@
 xit(
-30
+21
 )%0A%0A 
@@ -1201,133 +1201,66 @@
     
-if not os.access(steamcmd_dir, os.W_OK):%0A            print('Cannot get write access to ' + steamcmd_dir)%0A            exit(31)
+steamcmd_dir_fs.access('w') #check for write access in dir
 %0A%0A  
@@ -1427,19 +1427,21 @@
 hive to 
+%25s
 ' 
-+
+%25
  steamcm
@@ -1775,19 +1775,21 @@
 lled in 
+%25s
 ' 
-+
+%25
  steamcm
@@ -2095,281 +2095,112 @@
     
-if not os.path.exists(steamcmd):%0A            print('SteamCMD executable doesn%5C't exist. Install SteamCMD first')%0A            exit(32)%0A%0A        if not os.access(steamcmd, os.X_OK):%0A            print('SteamCMD script (' + steamcmd + ') is not executable')%0A            exit(31
+steamcmd_fs = FSCheck(steamcmd, 'SteamCMD')%0A%0A        steamcmd_fs.exists()%0A        steamcmd_fs.access('x'
 )%0A%0A 

Replace manual FS checks with FSCheck