import fabric.api as fab
import cuisine


def install(filename, user="root", append=False):
    """
    Installs crontab from a given cronfile
    """
    new_crontab = fab.run("mktemp fabixcron.XXXX")
    cuisine.file_upload(new_crontab, filename)
    if append is True:
        sorted_crontab = fab.run("mktemp fabixcron.XXXX")
        # When user have no crontab, then crontab command returns 1 error code
        with fab.settings(warn_only=True):
            fab.sudo("crontab -u {} -l >> {} 2> /dev/null".format(user, new_crontab))
        fab.sudo("sort -u -o {} {}".format(sorted_crontab, new_crontab))
        new_crontab = sorted_crontab
    fab.sudo("crontab -u {} {}".format(user, new_crontab))

import fabric.api as fab
import cuisine


def install(filename, user="root", append=False):
    """
    Installs crontab from a given cronfile
    """
    new_crontab = fab.run("mktemp fabixcron.XXXX")
    cuisine.file_upload(new_crontab, filename)
    if append is True:
        # When user have no crontab, then crontab command returns 1 error code
        with fab.settings(warn_only=True):
            fab.sudo("crontab -u {} -l 2> /dev/null | awk '!x[$0]++{{print $0}}' >> {}".format(user, new_crontab))
    fab.sudo("crontab -u {} {}".format(user, new_crontab))

Remove duplicate lines from cron file without sorting
Remove duplicate lines from cron file without sorting

  import fabric.api as fab
  import cuisine
  
  
  def install(filename, user="root", append=False):
      """
      Installs crontab from a given cronfile
      """
      new_crontab = fab.run("mktemp fabixcron.XXXX")
      cuisine.file_upload(new_crontab, filename)
      if append is True:
-         sorted_crontab = fab.run("mktemp fabixcron.XXXX")
          # When user have no crontab, then crontab command returns 1 error code
          with fab.settings(warn_only=True):
-             fab.sudo("crontab -u {} -l >> {} 2> /dev/null".format(user, new_crontab))
+             fab.sudo("crontab -u {} -l 2> /dev/null | awk '!x[$0]++{{print $0}}' >> {}".format(user, new_crontab))
-         fab.sudo("sort -u -o {} {}".format(sorted_crontab, new_crontab))
-         new_crontab = sorted_crontab
      fab.sudo("crontab -u {} {}".format(user, new_crontab))
  
Remove duplicate lines from cron file without sorting
## Code Before:
import fabric.api as fab
import cuisine


def install(filename, user="root", append=False):
    """
    Installs crontab from a given cronfile
    """
    new_crontab = fab.run("mktemp fabixcron.XXXX")
    cuisine.file_upload(new_crontab, filename)
    if append is True:
        sorted_crontab = fab.run("mktemp fabixcron.XXXX")
        # When user have no crontab, then crontab command returns 1 error code
        with fab.settings(warn_only=True):
            fab.sudo("crontab -u {} -l >> {} 2> /dev/null".format(user, new_crontab))
        fab.sudo("sort -u -o {} {}".format(sorted_crontab, new_crontab))
        new_crontab = sorted_crontab
    fab.sudo("crontab -u {} {}".format(user, new_crontab))

## Instruction:
Remove duplicate lines from cron file without sorting
## Code After:
import fabric.api as fab
import cuisine


def install(filename, user="root", append=False):
    """
    Installs crontab from a given cronfile
    """
    new_crontab = fab.run("mktemp fabixcron.XXXX")
    cuisine.file_upload(new_crontab, filename)
    if append is True:
        # When user have no crontab, then crontab command returns 1 error code
        with fab.settings(warn_only=True):
            fab.sudo("crontab -u {} -l 2> /dev/null | awk '!x[$0]++{{print $0}}' >> {}".format(user, new_crontab))
    fab.sudo("crontab -u {} {}".format(user, new_crontab))
